<?php

namespace App\Model\Admin;

use Illuminate\Database\Eloquent\Model;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use Auth;


class AdminLogin extends Model
{
    protected $table = "admin";
    public $timestamps = false;
    /**
    *$tmp 为随意数 更换验证码
    *return codeimg 
    *
    */
    public static function getCode($id)
    {
    	//生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 200, $height = 42, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('code', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
        
    }
     /**
    *
    *
    *
    */
     public static function getLogin($arr)
     {
            
        //判断用户名 
       $count = Login::where('name',$arr->name)->count();
        //dd($count);
        if($count == 0){
        return redirect('admin/login')->with('user', '用户名不存在');

       }
        //判断密码
        $pwd = md5($arr->password);
        $count1 = Login::where('name',$arr->name)->where('password',$pwd)->count();
        if(!$count1){
           return redirect('admin/login')->with('pwd', '密码不正确');
        }
        //判断验证码
        $code = Session::get('code');
            if($arr->input("code")!=$code){
              
              return redirect('admin/login')->with('code1', '验证码错误');
            }
        //验证成功 
        $admin = AdminLogin::where('name',$arr->name)->first()->original;
        //登陆次数自增
        AdminLogin::where('name',$arr->name)->update(['count'=>$admin['count']+1]);
        //登陆时间改变
       AdminLogin::where('name',$arr->name)->update(['time'=>time()]);
        // 将用户信息放入Session
       Session::push('admin',$admin);
        //$data = Session::get('admin');
        return redirect('admin/index');
    }


}
